//
//  JPMoviewPlayerController.m
//  Jpod
//
//  Created by zenox on 2022/4/18.
//

#import "JPMoviewPlayerController.h"

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

@interface JPMoviewPlayerController ()
{
    AVPlayerViewController      *_playerController;
          AVPlayer                    *_player;
          AVAudioSession              *_session;
//          NSString                    *_urlString;
}

/**
 *
 */
@property(nonatomic,strong)AVPlayerViewController *playerController;

@end

@implementation JPMoviewPlayerController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSString *webVideoPath = @"http://data.vod.itc.cn/?rb=1&key=jbZhEJhlqlUN-Wj_HEI8BjaVqKNFvDrn&prod=flash&pt=1&new=/137/113/vITnGttPQmaeWrZ3mg1j9H.mp4";
    NSURL *webVideoUrl = [NSURL URLWithString:webVideoPath];
//    _player = [AVPlayer playerWithURL:[NSURL URLWithString:path]];
//    _playerController.player = _player;

    
    self.playerController.player = [AVPlayer playerWithURL:[NSURL URLWithString:webVideoPath]];
    [self.playerController.player play];
    
    
}

- (AVPlayerViewController*)playerController{
    if (!_playerController) {
        _playerController = [[AVPlayerViewController alloc] init];
        _playerController.videoGravity = AVLayerVideoGravityResizeAspect;
        _playerController.allowsPictureInPicturePlayback = true;    //画中画，iPad可用
        _playerController.showsPlaybackControls = true;
//        [self addChildViewController:_playerController];
        _playerController.view.translatesAutoresizingMaskIntoConstraints = true;
        //AVPlayerViewController 内部可能是用约束写的，这句可以禁用自动约束，消除报错
        //self.view.bounds
  //      _playerController.view.frame = CGRectMake(0, 0, 320, 300);
        [self.view addSubview:_playerController.view];
      
      [_playerController.view mas_makeConstraints:^(MASConstraintMaker *make) {
          make.top.leading.mas_equalTo(self.view).offset(20);
          make.bottom.trailing.mas_equalTo(self.view).offset(-20);
      }];
        
    }
    
    return  _playerController;
}

- (void)creatVideoWithPath:(NSString *)path{
      
//      _session = [AVAudioSession sharedInstance];
//      [_session setCategory:AVAudioSessionCategoryPlayback error:nil];
      _player = [AVPlayer playerWithURL:[NSURL URLWithString:path]];
      _playerController = [[AVPlayerViewController alloc] init];
      _playerController.player = _player;
//      _playerController.view.backgroundColor = [UIColor colorWithHexString:@"#E0E5F0"];
      _playerController.videoGravity = AVLayerVideoGravityResizeAspect;
      _playerController.allowsPictureInPicturePlayback = true;    //画中画，iPad可用
      _playerController.showsPlaybackControls = true;
      
      [self addChildViewController:_playerController];
      _playerController.view.translatesAutoresizingMaskIntoConstraints = true;
      //AVPlayerViewController 内部可能是用约束写的，这句可以禁用自动约束，消除报错
      //self.view.bounds
//      _playerController.view.frame = CGRectMake(0, 0, 320, 300);
      [self.view addSubview:_playerController.view];
    
    [_playerController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.mas_equalTo(self.view).offset(20);
        make.bottom.trailing.mas_equalTo(self.view).offset(-20);
    }];
      [_playerController.player play];    //自动播放
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
